package com.mes.common.domain.condition;

import java.io.Serializable;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * <p>
 * 代表了条件语句的属性值对，如果该属性为string型则value前面要加上''，否则不加,应该把每一个对象的属性也当成static的值写在此处，以防止在输入的时候产生错误。
 * operator是is的时候要特殊处理
 * </p>
 * created on 2009-07
 * 
 * @author lhy
 * 
 */
/**
 * @time 2017/3/16
 * @author SnowBear
 * �����������ֵ�����ķ���Ϊɶ��Ҫ��һ������֣��������֣��ĸ����Ҳ�ð�
 * �еķ����Ѿ�������
 */
public class ConditionObject implements Serializable, IConditionObject {

	// 类型常量
	public final static String INTEGER_TYPE = "integer";
	public final static String LONG_TYPE = "long";
	public final static String STRING_TYPE = "string";
	public final static String DATE_TYPE = "date";
	public final static String BOOLEAN_TYPE = "boolean";
	public final static String FLOAT_TYPE = "float";
	public final static String DOUBLE_TYPE = "double";
	public final static String ATTIBUTE_TYPE = "attribute";
	// 操作符常量
	public final static String MORE_OPERATOR = ">";
	public final static String MORE_AND_EQUAL_OPERATOR = ">=";
	public final static String LESS_OPERATOR = "<";
	public final static String LESS_AND_EQUAL_OPERATOR = "<=";
	public final static String EQUAL_OPERATOR = "=";
	public final static String NOT_EQUAL_OPERATOR = "<>";
	public static final String LIKE_OPERATION_TYPE = "like";
	public final static String BY_OPERATOR = "by";
	public final static String IS_OPERATOR = "is";
	public final static String IS_NOT_OPERATOR = "is not";

	private String key;

	private String operator;

	private String value;

	private String keyType;

	public ConditionObject(String key, String operator, String value) {
		super();
		this.key = key;
		this.operator = operator;
		this.value = value;
		this.keyType = ConditionObject.STRING_TYPE;
	}

	public ConditionObject(String key, String operator, String value,
			String keyType) {
		super();
		this.key = key;
		this.operator = operator;
		this.value = value;
		this.keyType = keyType;
	}

	public ConditionObject() {
		super();
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String toString() {
		return key + " " + operator + " " + getWapValue();
	}

	/**
	 * 返回三个属性的连接，其中value用问号代替
	 * 
	 * @return
	 */
	public String toStringWithoutValue() {
		if ((ConditionObject.IS_OPERATOR.equalsIgnoreCase(this.operator.trim()) && "null"
				.equalsIgnoreCase(this.value.trim()))
				|| (ConditionObject.IS_NOT_OPERATOR
						.equalsIgnoreCase(this.operator.trim()) && "null"
						.equalsIgnoreCase(this.value.trim()))) {
			return toStringWithValue();
		}
		if(ConditionObject.ATTIBUTE_TYPE.equalsIgnoreCase(this.keyType.trim()))
			return toStringWithValue();
		return key + " " + operator + " ? ";
	}

	/**
	 * 返回三个属性的连接
	 * 
	 * @return
	 */
	public String toStringWithValue() {
		return key + " " + operator + " " + getWapValue();
	}

	public String getWapValue() {
		String wapValue = getValue();
		if ((ConditionObject.IS_OPERATOR.equalsIgnoreCase(this.operator.trim()))
				|| (ConditionObject.IS_NOT_OPERATOR
						.equalsIgnoreCase(this.operator.trim()) && "null"
						.equalsIgnoreCase(this.value.trim()))) {
			wapValue = getValue();
		} else if (ConditionObject.isKeyAttributeType(this)){
			wapValue = getValue();
		}else if (ConditionObject.isKeyStringType(this)) {
			wapValue = " '" + getValue() + "' ";
		}
		return wapValue;
	}

	public static String getValueByKeyFromList(List<ConditionObject> coList,
			String keyName) {
		if (coList == null) {
			return null;
		}
		Iterator iter = coList.iterator();
		String value = "";
		while (iter.hasNext()) {
			ConditionObject co = (ConditionObject) iter.next();
			if (co.getKey().equalsIgnoreCase(keyName)) {
				value = co.getValue();
				return value;
			}
		}
		return null;
	}

	public static boolean removeObjectByKeyFromList(
			List<ConditionObject> coList, String keyName) {
		int i = 0;
		Iterator iter = coList.iterator();
		while (iter.hasNext()) {
			ConditionObject co = (ConditionObject) iter.next();
			if (co.getKey().equalsIgnoreCase(keyName)) {
				coList.remove(i);
				return true;
			}
			i++;
		}
		return false;
	}

	public static boolean isContainKey(final List<ConditionObject> coList,
			String keyName) {
		if (coList == null || coList.size() == 0) {
			return false;
		}
		Iterator iter = coList.iterator();
		while (iter.hasNext()) {
			ConditionObject co = (ConditionObject) iter.next();
			if (co.getKey().equalsIgnoreCase(keyName)) {
				return true;
			}
		}
		return false;
	}

	public static String parseStringList(List inList) {
		StringBuffer sb = new StringBuffer();
		String temp = null;
		Iterator iter = inList.iterator();
		while (iter.hasNext()) {
			String element = (String) iter.next();
			sb.append("'" + element + "'" + ",");
		}
		temp = sb.substring(0, sb.length() - 1);
		return temp;
	}

	public static boolean isKeyStringType(ConditionObject conditionObject) {
		return "string".equalsIgnoreCase(conditionObject.keyType);
	}
	
	public static boolean isKeyAttributeType(ConditionObject conditionObject) {
		return "attribute".equalsIgnoreCase(conditionObject.keyType);
	}

	/**
	 * 得到参数值的一个包装类型
	 * 
	 * @param conditionObject
	 * @return
	 */
	public static Object getValueObject(ConditionObject conditionObject) {
		String temp = conditionObject.getValue();
		Object value = null;
		if (ConditionObject.STRING_TYPE.equalsIgnoreCase(conditionObject
				.getKeyType())) {
			// varchar型参数
			value = temp;
		} else if (ConditionObject.DATE_TYPE.equalsIgnoreCase(conditionObject
				.getKeyType())) {
			// 日期型参数
			value = new Date(temp);
		} else if (ConditionObject.FLOAT_TYPE.equalsIgnoreCase(conditionObject
				.getKeyType())) {
			// float型参数，
			value = new Float(temp);
		} else if (ConditionObject.DOUBLE_TYPE.equalsIgnoreCase(conditionObject
				.getKeyType())) {
			// double型参数，
			value = new Double(temp);
		} else if (ConditionObject.LONG_TYPE.equalsIgnoreCase(conditionObject
				.getKeyType())) {
			// long型参数，
			value = new Long(temp);
		} else if (ConditionObject.BOOLEAN_TYPE
				.equalsIgnoreCase(conditionObject.getKeyType())) {
			// long型参数，
			value = new Long(temp);
		} else if (ConditionObject.INTEGER_TYPE
				.equalsIgnoreCase(conditionObject.getKeyType())) {
			// int型参数，
			value = new Integer(temp);
		} else if (ConditionObject.ATTIBUTE_TYPE
				.equalsIgnoreCase(conditionObject.getKeyType())) {
			// int型参数，
			value = temp;
		} else {
			// 最后未知类型的按照字符型的处理
			value = temp;
		}
		return value;
	}

	public String getKeyType() {
		return keyType;
	}

	public void setKeyType(String keyType) {
		this.keyType = keyType;
	}

	public String getOperator() {
		return operator;
	}

	public void setOperator(String operator) {
		this.operator = operator;
	}
}
